package com.wellsoft.mobileoa.utils.view;

import java.util.ArrayList;
import java.util.List;

import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;
import com.wellsoft.mobileoa.utils.BaseView;
import com.wellsoft.mobileoa.utils.BaseWidget;
import com.wellsoft.mobileoa.utils.action.GoBackAction;
import com.wellsoft.mobileoa.wsmodel.WSOA_Action;
import com.wellsoft.mobileoa.wsmodel.WSOA_Page;
import com.wellsoft.mobileoa.wsmodel.WSOA_PostField;
import com.wmobile.utils.PopConfirmDialog;
import com.wmobile.utils.ServiceResponse;

public class FormView extends BaseView {

	public List<BaseWidget> widgetList;
	public ScrollView scrollView;

	public FormView(BaseActivity baseActivity, WSOA_Page page, String url) {
		super(baseActivity);
		this.baseActivity = baseActivity;
		this.page = page;
		this.url = url;
		

		hasEdited = false;
	}

	@Override
	public ViewGroup DrawSelf() {
		if (page.getPageFields() == null) {
			return null;
		}

		widgetList = new ArrayList<BaseWidget>();

		selfView = (ViewGroup) LayoutInflater.from(baseActivity).inflate(
				R.layout.formview, null);

		initTitleBar(selfView);

		scrollView = (ScrollView)selfView.findViewById(R.id.formview_scrollsv);
		
		LinearLayout widgetHorizontalLl = (LinearLayout) selfView
				.findViewById(R.id.formview_widgetcontainerll);
		for (int i = 0; i < page.getPageFields().size(); i++) {

			BaseWidget baseWidget = BaseWidget.createWidgetFactory(page
					.getPageFields().get(i), FormView.this);
			if (baseWidget != null) {
				widgetList.add(baseWidget);
				View view = baseWidget.getView();
				if (view != null) {
					widgetHorizontalLl.addView(view);
				}
			}
		}

		initToolBar(selfView);

		return selfView;

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			backOnClickListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	@Override
	public List<WSOA_PostField> getValue() {
		List<WSOA_PostField> postFields = new ArrayList<WSOA_PostField>();
		for (BaseWidget widget : widgetList) {
			postFields.add(widget.getValue());
		}
		return postFields;
	}

	@Override
	public void resetView() {
		for (BaseWidget widget : widgetList) {
			widget.resetWidget();
		}
	}

	@Override
	public BaseWidget getWidgetById(String widgetId) {
		for (int i = 0; i < widgetList.size(); i++) {
			if (widgetList.get(i).field.getFieldName() != null
					&& widgetList.get(i).field.getFieldName().equals(widgetId)) {
				return widgetList.get(i);
			}
		}
		return null;
	}

	@Override
	public ServiceResponse checkValid() {

		for (int i = 0; i < widgetList.size(); i++) {
			ServiceResponse response = widgetList.get(i).checkValid();
			if (!response.getExeFlag()) {
				return response;
			}
		}
		return new ServiceResponse(true);
	}

	@Override
	public void goBackActivity() {
		if (hasEdited) {
			PopConfirmDialog confirmDialog = new PopConfirmDialog(baseActivity,
					"表单已被修改，是否退出？");
			if (confirmDialog.showDialog() != PopConfirmDialog.DialogResultOK) {
				return;
			}
		}
		super.goBackActivity();
	}

}
